package com.wym.lottery.util;

import java.util.Calendar;

public class CheckScope {
	private static Calendar cal;
	private static int correction = 4;

	public static String getScope() {
		cal = Calendar.getInstance();
		//貌似设置一周的开始对android的运算值不会发生改变，这里肯定有点问题
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		int standard = cal.get(Calendar.WEEK_OF_YEAR) - 1;
		if(cal.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY) standard--;
		int offset = getOffset();
		int year = cal.get(Calendar.YEAR);
		return format2String(year, standard, offset, correction);
	}

	//得到一周的偏移量。
	//星期1-2，偏移1;星期3-5,偏移2;星期6-7,偏移3;
	private static int getOffset() {
		int day = cal.get(Calendar.DAY_OF_WEEK);
		int offset = 0;
		if (Calendar.MONDAY == day || day == Calendar.TUESDAY)
			offset = 1;
		if (Calendar.WEDNESDAY == day || Calendar.THURSDAY==day || day == Calendar.FRIDAY)
			offset = 2;
		if (Calendar.SATURDAY == day || day == Calendar.SUNDAY)
			offset = 3;
		return offset;

	}

	private static String format2String(int year, int standard, int offset, int correction) {
		int count = standard * 3 + offset - correction;
		String add;
		//如果期号只有两位数，就前缀补0
		if (count < 100)
			add = "0" + count + "";
		else
			add = count + "";
		String iyear = year + "";
		return (iyear + add).substring(2);
	}
}
